<template>
  <div class="app-container wechat-xcx">
    <div v-loading="$store.state.app.loading" class="table-main">
      <div class="title">系统配置</div>
      <el-alert type="info" :closable="false">该操作将直接影响终端程序执行结果，请谨慎操作。</el-alert>
      <el-form ref="itemData" size="small" :model="itemData" label-width="120px" style="width:500px">
        <div class="partition-container">
          <div class="partition-left">
            <el-form-item class="is-required" prop="sub_account" label="子账号售价：">
              <el-input v-model="itemData.sub_account" placeholder="设置终端销售子账号的价格" :disabled="isReadonly" onkeyup="this.value = this.value.replace(/[^\d.]/g,'')">
                <template slot="append">/ 天</template>
              </el-input>
            </el-form-item>
            <el-form-item class="is-required" prop="sub_account" label="防竞争密码：">
              <el-input v-model="itemData.check_in_pwd" placeholder="请设置防竞争密码" :disabled="isReadonly">
                <template slot="append">/ 天</template>
              </el-input>
            </el-form-item>
          </div>
        </div>
        <el-form-item>
          <template v-if="isReadonly">
            <el-button type="primary" :plain="true" @click="handleSubmitForm">修 改</el-button>
          </template>
          <template v-else>
            <el-button :plain="false" @click="fillItemData">取 消</el-button>
            <el-button type="primary" :plain="false" :loading="$store.state.app.loading" @click="handleSubmitForm">保 存</el-button>
          </template>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { batch, updateBatch } from '@/api/config'

export default {
  data: function() {
    return {
      isReadonly: true,
      params: { sub_account: null, check_in_pwd: null },
      itemData: {
        sub_account: null,
        check_in_pwd: null
      }
    }
  },
  mounted: function() {
    // 初始化方法
    this.initialize()
  },
  methods: {
    // 初始化数据列表
    initialize: function() {
      // 获取用户列表
      this.$store.commit('app/SET_LOADING', true)
      batch(['sub_account', 'check_in_pwd']).then(res => {
        this.params.sub_account = res.data.sub_account.value
        this.params.check_in_pwd = res.data.check_in_pwd.value
        this.fillItemData()
      })
    },
    // 打开数据编辑窗口
    handleSubmitForm: function() {
      if (!this.isReadonly) {
        this.$store.commit('app/SET_LOADING', true)
        updateBatch({
          keys: Object.keys(this.itemData),
          values: Object.values(this.itemData)
        }).then(function(res) {
          this.params.sub_account = res.data.sub_account.value
          this.params.check_in_pwd = res.data.check_in_pwd.value
          this.fillItemData()
        }.bind(this))
      } else {
        this.isReadonly = false
      }
    },
    // 填充数据
    fillItemData: function() {
      this.itemData.sub_account = this.params.sub_account ? parseFloat(this.params.sub_account) : null
      this.itemData.check_in_pwd = this.params.check_in_pwd
      this.isReadonly = true
      this.$nextTick(function() {
        this.$store.commit('app/SET_LOADING', false)
      })
    }
  }
}
</script>

<style lang="scss">
.wechat-xcx {
  .el-form-item label {
    font-weight: normal;
  }
  .partition-right {
    border: none !important;
    .el-form-item__content {
      margin-left: 0 !important;
    }
  }
}
</style>

<style lang="scss" scoped>
.app-container {
  letter-spacing: 0.5px;
  > .title {
    color: #333333;
    font-size: 14px;
    font-weight: 700;
    padding-left: 8px;
    border-left: 2px solid #88B7E0;
  }
  .el-alert {
    border-radius: 0;
    margin: 20px 0 20px 0;
    border: 1px solid #EBEEF5;
  }
}
</style>
